package cetcbigdata.da.service.xinjiang.spider1;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class Spider1 extends Base {

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        String themeListUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/auditQzqdRest/getPowerTypeName";
        String text = "{'token':'Epoint_WebSerivce_**##0601','params':{'catalogcode':''}}";
        JSONObject themeListParams = new JSONObject();
        themeListParams.put("params", JSONObject.parseObject(text));
        String themeListStrResp = postReq(themeListUrl, themeListParams);
        JSONObject themeListResp = JSONObject.parseObject(themeListStrResp);
        JSONArray powerTypeList = themeListResp.getJSONObject("custom").getJSONArray("powerTypeList");
        for (Object o : powerTypeList){
            JSONObject object = (JSONObject) o;
            String powertype = object.getString("powertype");
            //当前页和总页数
            int currentPage = 0;
            int pags = currentPage + 1;
            while (pags > currentPage) {
                try {
                    text = "{'token':'Epoint_WebSerivce_**##0601','params':{'pageindex':"+currentPage+",'pagesize':5,'search':'','powertype':'"+powertype+"','catalogcode':''}}";
                    String themeUrl = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/auditQzqdRest/getQzqdNameByPowertype";
                    JSONObject themeParams = JSONObject.parseObject(text);
                    String themeStrResp = postReq(themeUrl, themeParams);
                    JSONObject themeResp = JSONObject.parseObject(themeStrResp);
                    JSONObject custom = themeResp.getJSONObject("custom");
                    JSONArray qzqdlist = custom.getJSONArray("qzqdlist");
                    for (Object o1 : qzqdlist){
                        JSONObject qzqd = (JSONObject) o1;

                        String rowguid = qzqd.getString("rowguid");
                        String taskname = qzqd.getString("taskname");
                        String calalogcode = qzqd.getString("calalogcode");

                        text = "{'token':'Epoint_WebSerivce_**##0601','params':{'pageindex':0,'pagesize':10,'rowguid':'"+rowguid+"','deptcode':''}}";
                        String url = "https://zwfw.xinjiang.gov.cn/xjzwdt/rest/auditQzqdRest/getQzqdInfoByCatalogCode";
                        JSONObject params = JSONObject.parseObject(text);
                        String strResp = postReq(url, params);
                        JSONObject resp = JSONObject.parseObject(strResp);

                        //结果封装
                        //名称、地址
                        JSONObject result = new JSONObject();
                        String dataUrl = "https://zwfw.xinjiang.gov.cn/xjzwfw2021/qzqdnew/list_details.html?calalogcode="+calalogcode+";&rowguid="+rowguid+"&taskname="+taskname;
                        result.put("职权名称", qzqd.getString("taskname"));
                        result.put("职权部门", qzqd.getString("ouname"));
                        result.put("地址", dataUrl);
                        //基础信息
                        JSONObject taskinfo = resp.getJSONObject("custom").getJSONObject("taskinfo");
                        JSONObject taskinfoRes = new JSONObject();
                        taskinfoRes.put("行政主体",taskinfo.getString("ouname"));
                        taskinfoRes.put("类别", getTheme(taskinfo.getString("powertype")));
                        taskinfoRes.put("编码", taskinfo.getString("catalogcode"));
                        taskinfoRes.put("责任事项依据", taskinfo.getString("taskbasis"));
                        taskinfoRes.put("责任事项内容", taskinfo.getString("taskcontent"));
                        taskinfoRes.put("追责对象范围", taskinfo.getString("zzdxscope"));
                        taskinfoRes.put("追责情形", taskinfo.getString("zzsituation"));
                        result.put("基础信息", taskinfoRes);
                        //实施清单
                        JSONArray datas = resp.getJSONObject("custom").getJSONArray("data");
                        if (null != datas && datas.size() > 0) {
                            JSONArray ssqdList = new JSONArray();
                            for (Object o2 : datas) {
                                JSONObject data = (JSONObject) o2;
                                JSONObject ssqd = new JSONObject();
                                String taskcode = data.getString("taskcode");
                                ssqd.put("服务事项名称", data.getString("catalogname"));
                                ssqd.put("办理机构", data.getString("deptname"));
                                ssqd.put("操作指南", "https://zwfw.xinjiang.gov.cn/bmfwtest/guidetest/guidance.html?taskcode="+taskcode);
                                ssqdList.add(ssqd);
                            }
                            result.put("实施清单", ssqdList);
                        }else {
                            result.put("实施清单", null);
                        }
                        String areaName = "新疆维吾尔自治区";
                        System.out.println(
                                currentPage + "\n" +
                                result + "\n"
                        );
                        return processResult(
                                "https://zwfw.xinjiang.gov.cn/xjzwfw2021/qzqdnew/responsibility_list.html",
                                themeListStrResp,
                                dataUrl,
                                strResp,
                                result,
                                areaName,
                                "权责清单-"+getTheme(powertype)
                        );
                    }
                    //更新当前页和总页数
                    currentPage ++;
                    pags = (int) Math.ceil((double)custom.getInteger("total") / 5);
                } catch (Exception e) {
                    logger.info("无数据");
                }
            }
        }

        return null;
    }

    private String getTheme(String powertype){
        switch (powertype){
            case "01":return "行政许可";
            case "02":return "行政处罚";
            case "03":return "行政强制";
            case "04":return "行政征收";
            case "05":return "行政给付";
            case "06":return "行政检查";
            case "07":return "行政确认";
            case "08":return "行政奖励";
            case "09":return "行政裁决";
            case "10":return "其他行政权力";
            case "20":return "公共服务";
            default:
                return null;
        }
    }

    public static void main(String[] args) {
        Spider1 list1 = new Spider1();
        list1.getDataDemo();
    }
}
